import React, { Component } from "react";
import { Link, Route, Switch } from "react-router-dom";

class Home extends Component {
  render() {
    return <div>这是home组件</div>;
  }
}

class About extends Component {
  render() {
    return <div>这是About组件</div>;
  }
}

const Mine = () => {
  return <div>这是我的组件</div>;
};

const Detail = (props) => {
  // console.log(props.match.params.id);
  // ?.叫做可选链操作符

  // const obj = {
  //   a: 3,
  // fn() {
  //   console.log("fn");
  // },
  // };
  // console.log(obj.b?.x);

  // const fn = () => {
  //   console.log("fn");
  // };
  // obj.fn?.();

  const {
    params: { id },
  } = props.match;

  // return <div>这是详情组件 - {props.match?.params?.id}</div>;
  return <div>这是详情组件 - {id}</div>;
};

class App extends Component {
  state = {};
  render() {
    return (
      <>
        <h2>react-router-params</h2>
        <ul>
          <li>
            <Link to="/home">首页</Link>
          </li>
          <li>
            <Link to="/about">关于</Link>
          </li>
          <li>
            <Link to="/mine">我的</Link>
          </li>
          <li>
            <Link to="/detail/2345">详情页</Link>
          </li>
        </ul>

        <hr />

        <Switch>
          {/* 动态路由的做法和vue一样，要在路径上面加/:id */}
          <Route path="/home" component={Home}></Route>
          <Route path="/about" component={About}></Route>
          <Route path="/mine" component={Mine}></Route>
          <Route path="/detail/:id" component={Detail}></Route>
        </Switch>
      </>
    );
  }
}

export default App;
